package com.xiaoshuidi.cloud.module.report.dal.mapper.report;

import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import com.xiaoshuidi.cloud.framework.mybatis.core.mapper.BaseMapperX;
import com.xiaoshuidi.cloud.module.report.controller.finance.vo.OwnerDetailIncomePageReqVO;
import com.xiaoshuidi.cloud.module.report.dal.DO.report.OwnerIncomeDetailReportDO;
import com.xiaoshuidi.cloud.module.report.dal.DO.report.OwnerIncomeSummaryReportDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.List;

@Mapper
@InterceptorIgnore(tenantLine = "true")
public interface OwnerIncomeDetailReportMapper extends BaseMapperX<OwnerIncomeDetailReportDO> {

    @Select("select count(id)\n" +
             " from report_owner_income_detail\n" +
            " where statistics_month = #{currentMonth}")
    long countDetailsInCurrentMonth(@Param("currentMonth")String currentMonth);

    long countListForPage(@Param("req") OwnerDetailIncomePageReqVO reqVO);

    List<OwnerIncomeDetailReportDO> selectListForPage(@Param("req") OwnerDetailIncomePageReqVO reqVO);

    List<OwnerIncomeDetailReportDO> selectListForExport(@Param("req") OwnerDetailIncomePageReqVO reqVO);

    List<OwnerIncomeDetailReportDO> selectListByContractIdAndStatisMonth(@Param("month") String month,@Param("roomId") Long roomId);

    @Select("select * " +
            " from report_owner_income_detail " +
            " where contract_id = #{contractId} and statistics_month = #{currentMonth} limit 1")
    OwnerIncomeDetailReportDO selectDetailsByContractIdInCurrentMonth(@Param("contractId")Long contractId, @Param("currentMonth")String currentMonth);


    @Select("select sum(cur_rent_reduce) amount " +
            " from report_owner_income_detail " +
            " where contract_id = #{contractId} ")
    BigDecimal sumTotalRentAndWaterByContractId(@Param("contractId")Long contractId);

    @Select("select apartment_id apartmentId,area area,statistics_month statisticsMonth," +
            " sum(cur_rent_reduce) rentIn,sum(cur_other_in) otherIn,sum(acc_rent_reduce) sumIn ,tenant_id tenantId," +
            " dept_id deptId from report_owner_income_detail where statistics_month = #{currentMonth} GROUP BY area")
    List<OwnerIncomeSummaryReportDO> selectDetailsInCurrentMonthGroupByArea(@Param("currentMonth")String currentMonth);


}
